﻿using System;
using System.Collections.Generic;
using System.Linq;
using Emgu.CV.Structure;
using Emgu.CV;
using System.Drawing;

namespace iMapa.Backend
{
    public class InteractMapGroup
    {
        private string name;
        private List<InteractMap> maps = new List<InteractMap>();

        public List<InteractMap> Maps
        {
            get { return maps; }
            set { maps = value; }
        }
       
        public string Name
        {
            get { return name; }
            set { name = value; }
        }

        public List<InteractMap> CurrentDetected()
        {
            List<InteractMap> current = new List<InteractMap>();
            foreach (InteractMap m in maps)
            {
                if (m.HMatrixToObserved != null)
                {
                    current.Add(m);
                }
            }

            return current;
        }

        public List<InteractMap> GetAllInteractMaps()
        {
            return maps;
        }

        public Image<Gray, float> GetDetectionMask(Size i)
        {
            Image<Gray, float> mask = new Image<Gray, float>(i.Width, i.Height, new Gray(0.0));
            foreach (InteractMap map in CurrentDetected())
            {
                lock (map)
                {
                    if (map.HMatrixToObserved != null)
                    {
                        Rectangle rect = map.ModelImage.ROI;
                        PointF[] pts = new PointF[] { 
                        new PointF(rect.Left, rect.Bottom),
                        new PointF(rect.Right, rect.Bottom),
                        new PointF(rect.Right, rect.Top),
                        new PointF(rect.Left, rect.Top)};
                        map.HMatrixToObserved.ProjectPoints(pts);

                        mask.FillConvexPoly(Array.ConvertAll<PointF, Point>(pts, Point.Round), new Gray(255));
                    }
                }
            }
            return mask;
        }

        public void ResetHomographies()
        {
            foreach (InteractMap map in CurrentDetected())
            {
                map.resetHomography();
            }

        }
    }
}
